package cn.cmxds.store.widget;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.v7.widget.TintTypedArray;
import android.support.v7.widget.Toolbar;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import cn.cmxds.store.R;

public class MyToolBar extends Toolbar {

    private LayoutInflater mInflater;
    private View mView;
    private TextView mTextTitle;
    private EditText mSearchView;
    private Button mRightButton;
    private ImageButton mLeftImgButton;
    private ImageButton mRightImgButton;

    public MyToolBar(Context context) {
        this(context,null);
    }

    public MyToolBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    @SuppressLint("RestrictedApi")
    public MyToolBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
        setContentInsetsRelative(10,10);
        if (attrs != null) {
            /**
             * 读取自定义属性
             */
            final TintTypedArray a = TintTypedArray.obtainStyledAttributes(getContext(), attrs,
                    R.styleable.MyToolBar, defStyleAttr, 0);
            //左边按钮图片
            final Drawable leftButtonIcon = a.getDrawable(R.styleable.MyToolBar_leftButtonIcon);
            if (leftButtonIcon != null) {
                setLeftButtonIcon(leftButtonIcon);
            }
            //右边按钮图片
            final Drawable rightImgButtonIcon = a.getDrawable(R.styleable.MyToolBar_rightButtonIcon);
            if (rightImgButtonIcon != null) {
                setRightButtonIcon(rightImgButtonIcon);
            }
            //右边按钮文字
            CharSequence rightButtonText = a.getText(R.styleable.MyToolBar_rightButtonText);
            if (rightButtonText != null) {
                setRightButtonText(rightButtonText);
            }
            //搜索框
            boolean isShowSearchview = a.getBoolean(R.styleable.MyToolBar_isShowSearchView, false);
            if (isShowSearchview) {
                showSearchView();
                hideTitleView();
                hideRightButton();
                hideRightImgButton();
                hideLeftButton();
            } else {
                hideSearchView();
                showTitleView();
                showRightButton();
                showRightImgButton();
                showLeftButton();
            }
            //资源回收
            a.recycle();
        }
    }




    /**
     * 初始化控件
     */
    private void initView() {
        //避免控件添加进去为空，setTitle()在构造函数没有调用，因此可能为空，需要为空判断
        if (mView == null) {
            mInflater = LayoutInflater.from(this.getContext());
            mView = mInflater.inflate(R.layout.toolbar, null);
            mLeftImgButton = mView.findViewById(R.id.toolbar_left_button);
            mTextTitle = mView.findViewById(R.id.toolbar_title);
            mSearchView = mView.findViewById(R.id.toolbar_searchview);
            mRightButton = mView.findViewById(R.id.toolbar_right_button);
            mRightImgButton = mView.findViewById(R.id.toolbar_right_imgbutton);
            mSearchView.setOnFocusChangeListener(new OnFocusChangeListener() {
                @Override
                public void onFocusChange(View v, boolean hasFocus) {
                    if (hasFocus) {
                        mSearchView.setHint("");
                        mSearchView.setGravity(Gravity.LEFT);
                        mSearchView.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
                    }else{
                        mSearchView.setGravity(Gravity.CENTER_HORIZONTAL);
                        mSearchView.setHint("请输入搜索内容");
                    }
                }
            });


            //layout布局
            LayoutParams lp = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL);

            //将控件添加到toolbar
            addView(mView, lp);
        }
    }

    /**
     * 左按键图标
     * @param icon
     */
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    public void  setLeftButtonIcon(Drawable icon){

        if(mLeftImgButton!=null){

            mLeftImgButton.setBackground(icon);
            mLeftImgButton.setVisibility(VISIBLE);
        }

    }

    public void  setLeftButtonIcon(int icon){

        setLeftButtonIcon(getResources().getDrawable(icon));
    }

    /**
     * 右按键图标
     * @param icon
     */
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    public void  setRightButtonIcon(Drawable icon){

        if(mRightImgButton !=null){

            mRightImgButton.setBackground(icon);
            mRightImgButton.setVisibility(VISIBLE);
        }

    }

    public void  setRightButtonIcon(int icon){

        setRightButtonIcon(getResources().getDrawable(icon));
    }


    /**
     * 右侧按键点击事件
     *
     */
    public  void setRightButtonOnClickListener(OnClickListener li){

        mRightButton.setOnClickListener(li);
    }

    /**
     * 右侧图片按键点击事件
     *
     */
    public void setmRightImgButtonOnClickListener(OnClickListener li){
        mRightImgButton.setOnClickListener(li);

    }

    /**
     * 左侧图片按键点击事件
     *
     */
    public void setmLeftImgButtonOnClickListener(OnClickListener li){
        mLeftImgButton.setOnClickListener(li);
    }
    public void setmTextTitleOnClickListener(OnClickListener li)
    {
        mTextTitle.setOnClickListener(li);
    }
    /**右按键文字
     * @param text
     */
    public void setRightButtonText(CharSequence text){
        mRightButton.setText(text);
        mRightButton.setVisibility(VISIBLE);
    }

    public void setRightButtonText(int id){
        setRightButtonText(getResources().getString(id));
    }



    public Button getRightButton(){ return this.mRightButton; }
    public ImageButton getLeftImageButton(){
        return this.mLeftImgButton;
    }
    public ImageButton getmRightImgButton(){
        return this.mRightImgButton;
    }



    @Override
    public void setTitle(int resId) {

        setTitle(getContext().getText(resId));
    }

    @Override
    public void setTitle(CharSequence title) {

        initView();
        if(mTextTitle !=null) {
            mTextTitle.setText(title);
            showTitleView();
        }





    }


    /**
     * 显示搜索框
     */
    public  void showSearchView(){

        if(mSearchView !=null)
            mSearchView.setVisibility(VISIBLE);

    }


    /**
     * 隐藏搜索框
     */
    public void hideSearchView(){
        if(mSearchView !=null)
            mSearchView.setVisibility(GONE);
    }

    /**
     * 显示标题栏
     */
    public void showTitleView(){
        if(mTextTitle !=null)
            mTextTitle.setVisibility(VISIBLE);
    }


    /**
     * 隐藏标题栏
     */
    public void hideTitleView() {
        if (mTextTitle != null)
            mTextTitle.setVisibility(GONE);

    }

    /**
     * 隐藏左按键
     */
    private void hideLeftButton() {
        if (mLeftImgButton != null)
            mLeftImgButton.setVisibility(GONE);

    }

    /**
     * 隐藏右按键
     */
    private void hideRightButton() {
        if (mRightButton != null)
            mRightButton.setVisibility(GONE);
    }

    /**
     * 隐藏右图片按键
     */
    private void hideRightImgButton() {
        if (mRightImgButton != null)
            mRightImgButton.setVisibility(GONE);
    }

    /**
     * 展示左按键
     */
    private void showLeftButton() {
        if(mLeftImgButton !=null)
            mLeftImgButton.setVisibility(VISIBLE);
    }

    /**
     * 展示右图片按键
     */
    private void showRightImgButton() {
        if(mRightImgButton !=null)
            mRightImgButton.setVisibility(VISIBLE);
    }

    /**
     * 展示右按键
     */
    private void showRightButton() {
        if(mRightButton !=null)
            mRightButton.setVisibility(VISIBLE);
    }

}
